package vip.xiaozhao.paotui.controller.paituiList;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import vip.xiaozhao.paotui.controller.BaseController;
import vip.xiaozhao.paotui.intf.dto.PaoTuiDetailDO;
import vip.xiaozhao.paotui.intf.dto.PaoTuiSelectionDO;
import vip.xiaozhao.paotui.intf.dto.ResponseDO;
import vip.xiaozhao.paotui.intf.entity.PaotuiCollege;
import vip.xiaozhao.paotui.intf.entity.PaotuiDetail;
import vip.xiaozhao.paotui.intf.entity.PaotuiDetailAudit;
import vip.xiaozhao.paotui.intf.enums.*;
import vip.xiaozhao.paotui.intf.service.PaotuiAuditService;
import vip.xiaozhao.paotui.intf.service.PaotuiCollegeService;
import vip.xiaozhao.paotui.intf.service.PaotuiDetailService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;

import static vip.xiaozhao.paotui.intf.constant.PageSizeConstant.PAOTUI_LIST_SIZE;

@RestController
@RequestMapping("/paotui/")
@Slf4j
public class PaotuiListController extends BaseController {

    @Resource
    private PaotuiDetailService paotuiDetailService;
    @Resource
    private PaotuiCollegeService paotuiCollegeService;

    @RequestMapping("/getList")
    public ResponseDO getList(PaoTuiSelectionDO paoTuiSelectionDO, int pageId, HttpServletRequest request, HttpServletResponse response) {
        log.info("get paoTuiList, PaoTuiSelectionDO: {} ,pageId: {}", paoTuiSelectionDO,pageId);
        if (pageId < 1) {
            return ResponseDO.fail("分页参数错误");
        }
        String ret = verifyParams(paoTuiSelectionDO);
        if (!SUCCESS.equals(ret)) {
            return ResponseDO.fail(ret);
        }

        PaoTuiDetailDO paoTuiDetailDO = paotuiDetailService.getList(paoTuiSelectionDO, pageId, PAOTUI_LIST_SIZE );

        return ResponseDO.success(paoTuiDetailDO);
    }

    @RequestMapping("/getNextList")
    @ResponseBody
    public ResponseDO getNextList(PaoTuiSelectionDO paoTuiSelectionDO,int lastId) {
        log.info("get paoTui NextList, PaoTuiSelectionDO: {} ,lastId: {}", paoTuiSelectionDO,lastId);
        String ret = verifyParams(paoTuiSelectionDO);

        if (lastId < 1) {
            log.info("getNextList lastId < 1");
            return ResponseDO.fail("传参错误");
        }
        if (!SUCCESS.equals(ret)) {
            return ResponseDO.fail(ret);
        }


        PaoTuiDetailDO paoTuiDetailDO = paotuiDetailService.getNextList(paoTuiSelectionDO, lastId,PAOTUI_LIST_SIZE);
        return ResponseDO.success(paoTuiDetailDO);
    }



    private String verifyParams(PaoTuiSelectionDO paoTuiSelectionDO){
        PaotuiCollege college = paotuiCollegeService.loadById(paoTuiSelectionDO.getCollegeId());
        if (college == null || college.getStatus() == 0) {
            log.info("college == null || college.getStatus() == 0");
            return "学校信息有误";
        }
        PaotuiSortTypeEnum sortTypeEnum = PaotuiSortTypeEnum.getById(paoTuiSelectionDO.getSort());
        if (sortTypeEnum == null) {
            log.info("sortTypeEnum == null");
            return "排序项有误";
        }
        PaotuiTimeTypeEnum timeTypeEnum = PaotuiTimeTypeEnum.getById(paoTuiSelectionDO.getTime());
        if (timeTypeEnum == null) {
            log.info("timeTypeEnum == null");
            return "送达时间有误";
        }
        PaotuiWeightEnum weightEnum = PaotuiWeightEnum.getById(paoTuiSelectionDO.getWeight());
        if (weightEnum == null) {
            log.info("weightEnum == null");
            return "物品重量有误";
        }
        PaotuiTypeEnum typeEnum = PaotuiTypeEnum.getById(paoTuiSelectionDO.getType());
        if (typeEnum == null) {
            log.info("typeEnum == null");
            return "物品种类有误";
        }
        PaotuiNumberEnum numberEnum = PaotuiNumberEnum.getById(paoTuiSelectionDO.getNum());
        if (numberEnum == null) {
            log.info("numberEnum == null");
            return "物品数量有误";
        }

        return SUCCESS;
    }



}
